/**
 * Ajax upload
 * Project page - http://valums.com/ajax-upload/
 * Copyright (c) 2008 Andris Valums, http://valums.com
 * Licensed under the MIT license (http://valums.com/mit-license/)
 * Version 2.3 (03.03.2009)
 */

(function(){
	
var d = document, w = window;

/**
 * Get element by id
 */	
function $(element){
	if (typeof element == "string")
		element = d.getElementById(element);
	return element;
}

/**
 * Attaches event to a dom element
 */
function addEvent(el, type, fn){
	if (w.addEventListener){
		el.addEventListener(type, fn, false);
	} else if (w.attachEvent){
		var f = function(){
		  fn.call(el, w.event);
		};			
		el.attachEvent('on' + type, f)
	}
}


/**
 * Creates and returns element from html chunk
 */
var toElement = function(){
	var div = d.createElement('div');
	return function(html){
		div.innerHTML = html;
		var el = div.childNodes[0];
		div.removeChild(el);
		return el;
	}
}();

function hasClass(ele,cls){
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
function addClass(ele,cls) {
	if (!hasClass(ele,cls)) ele.className += " "+cls;
}
function removeClass(ele,cls) {
	var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
	ele.className=ele.className.replace(reg,' ');
}

function getOffset(el){
	if (w.jQuery){
		return jQuery(el).offset();
	}
			
	var top = 0, left = 0;
	do {
		top += el.offsetTop  || 0;
		left += el.offsetLeft || 0;
	} while (el = el.offsetParent);
					
	return {
		left : left,
		top : top
	};
}

function getBox(el){
	var left, right, top, bottom;	
	var offset = getOffset(el);
	left = offset.left;
	top = offset.top;
	right = left + el.offsetWidth;
	bottom = top + el.offsetHeight;		
			
	return {
		left: left,
		right: right,
		top: top,
		bottom: bottom
	};
}

/**
 * Crossbrowser mouse coordinates
 */
function getMouseCoords(e){		
	// pageX/Y is not supported in IE
	// http://www.quirksmode.org/dom/w3c_cssom.html			
	if (!e.pageX && e.clientX){
		return {
			x: e.clientX + d.body.scrollLeft + d.documentElement.scrollLeft,
			y: e.clientY + d.body.scrollTop + d.documentElement.scrollTop
		};
	}

	return {
		x: e.pageX,
		y: e.pageY
	};		

}
/**
 * Function generates unique id
 */		
var getUID = function(){
	var id = 0;
	return function(){
		return 'ValumsAjaxUpload' + id++;
	}
}();

function fileFromPath(file){
	return file.replace(/.*(\/|\\)/, "");
}

function getExt(file){
	return (/[.]/.exec(file)) ? /[^.]+$/.exec(file.toLowerCase()) : '';
}

	// Please use AjaxUpload , Ajax_upload will be removed in the next version
	Ajax_upload = AjaxUpload = function(button, options){
		if (button.jquery){
			// jquery object was passed
			button = button[0];
		} else if (typeof button == "string" && /^#.*/.test(button)){					
			button = button.slice(1);				
		}
		button = $(button);
		
		this._input = null;
		this._button = button;
		this._disabled = false;
		this._submitting = false;
				
		this._settings = {
			// Location of the server-side upload script
			action: 'upload.php',
			// File upload name
			name: 'userfile',
			// Additional data to send
			data: {},
			// Submit file as soon as it's selected
			autoSubmit: true,
			// When user selects a file, useful with autoSubmit disabled			
			onChange: function(file, extension){},					
			// Callback to fire before file is uploaded
			// You can return false to cancel upload
			onSubmit: function(file, extension){},
			// Fired when file upload is completed
			onComplete: function(file, response) {}
		};

		// Merge the users options with our defaults
		for (var i in options) {
			this._settings[i] = options[i];
		}
		
		this._createInput();
		this._rerouteClicks();
	}
		
// assigning methods to our class
AjaxUpload.prototype = {
	setData : function(data){
		this._settings.data = data;
	},
	disable : function(){
		this._disabled = true;
	},
	enable : function(){
		this._disabled = false;
	},
	// use setData instead, set_data will be removed in the next version
	set_data : function(data){
		this.setData(data);
	},
	// removes ajaxupload
	destroy : function(){
		if(this._input){
			if(this._input.parentNode){
				this._input.parentNode.removeChild(this._input);
			}
			this._input = null;
		}
	},				
	/**
	 * Creates invisible file input above the button 
	 */
	_createInput : function(){
		var self = this;
		
		var input = d.createElement("input");
		input.setAttribute('type', 'file');
		input.setAttribute('name', this._settings.name);
		var styles = {
			'position' : 'absolute'
			,'margin': '-5px 0 0 -175px'
			,'padding': 0
			,'width': '220px'
			,'height': '10px'								
			,'opacity': 0
			,'cursor': 'pointer'
			,'display' : 'none'
			,'zIndex' :  2147483583 //Max zIndex supported by Opera 9.0-9.2x 
			// Strange, I expected 2147483647					
		};
		for (var i in styles){
			input.style[i] = styles[i];
		}
		
		// Make sure that element opacity exists
		// (IE uses filter instead)
		if ( ! (input.style.opacity === "0")){
			input.style.filter = "alpha(opacity=0)";
		}					
		d.body.appendChild(input);	
		
		addEvent(input, 'change', function(){
			// get filename from input
			var file = fileFromPath(this.value);	
			if(self._settings.onChange.call(self, file, getExt(file)) == false ){
				return;				
			}														
			// Submit form when value is changed
			if (self._settings.autoSubmit){
				self.submit();						
			}						
		});
		
		this._input = input;
	},
	_rerouteClicks : function (){
		var self = this;
	
		// IE displays 'access denied' error when using this method
		// other browsers just ignore click()
		// addEvent(this._button, 'click', function(e){
		//   self._input.click();
		// });				

		var box, over = false;			
		addEvent(self._button, 'mouseover', function(e){
			if (!self._input || over) return;
			over = true;
			box = getBox(self._button);								
		});
		
		// we can't use mouseout on the button,
		// because invisible input is over it
		addEvent(document, 'mousemove', function(e){
			var input = self._input;
			if (!input || !over) return;
			if (self._disabled){
				removeClass(self._button, 'hover');
				input.style.display = 'none';
				return;
			}
										
			var c = getMouseCoords(e);
					
			if ((c.x >= box.left) && (c.x <= box.right) && 
			(c.y >= box.top) && (c.y <= box.bottom)){
			
				input.style.top = c.y + 'px';
				input.style.left = c.x + 'px';
				input.style.display = 'block';
				addClass(self._button, 'hover');				
			} else {
				// mouse left the button
				over = false;
				input.style.display = 'none';
				removeClass(self._button, 'hover');
			}			
		});			
			
	},
	/**
	 * Creates iframe with unique name
	 */
	_createIframe : function(){
		// unique name
		// We cannot use getTime, because it sometimes return
		// same value in safari :(
		var id = getUID();
		
		// Remove ie6 "This page contains both secure and nonsecure items" prompt 
		// http://tinyurl.com/77w9wh
		var iframe = toElement('<iframe src="javascript:false;" name="' + id + '" />');
		iframe.id = id;
		iframe.style.display = 'none';
		d.body.appendChild(iframe);			
		return iframe;						
	},
	/**
	 * Upload file without refreshing the page
	 */
	submit : function(){
		var self = this, settings = this._settings;	
					
		if (this._input.value === ''){
			// there is no file
			return;
		}
										
		// get filename from input
		var file = fileFromPath(this._input.value);			

		// execute user event
		if (! (settings.onSubmit.call(this, file, getExt(file)) == false)) {
			// Create new iframe for this submission
			var iframe = this._createIframe();
			
			// Do not submit if user function returns false										
			var form = this._createForm(iframe);
			form.appendChild(this._input);

			form.submit();

			d.body.removeChild(form);				
			form = null;
			this._input = null;
			
			// create new input
			this._createInput();
			
			var toDeleteFlag = false;
			addEvent(iframe, 'load', function(){
				if (iframe.src == "about:blank"){						
					// First time around, do not delete.
					if( toDeleteFlag ){
						// Fix busy state in FF3
						setTimeout( function() {
							d.body.removeChild(iframe);
						}, 0);
					}
					return;
				}				
				
				var doc = iframe.contentDocument ? iframe.contentDocument : frames[iframe.id].document;
				var response = doc.body.innerHTML;
									
				settings.onComplete.call(self, file, response);
				
				// Reload blank page, so that reloading main page
				// does not re-submit the post. Also, remember to
				// delete the frame
				toDeleteFlag = true;				
				iframe.src = "about:blank"; //load event fired									
			});
		} else {
			// clear input to allow user to select same file
			this._input.value = '';						
		}
	},		
	/**
	 * Creates form, that will be submitted to iframe
	 */
	_createForm : function(iframe){
		var settings = this._settings;
		
		// method, enctype must be specified here
		// because changing this attr on the fly is not allowed in IE 6/7		
		var form = toElement('<form method="post" enctype="multipart/form-data"></form>');
		form.style.display = 'none';
		form.action = settings.action;
		form.target = iframe.name;
		d.body.appendChild(form);
		
		// Create hidden input element for each data key
		for (var prop in settings.data){
			var el = d.createElement("input");
			el.type = 'hidden';
			el.name = prop;
			el.value = settings.data[prop];
			form.appendChild(el);
		}
		return form;
	}	
};



})();


(function($){
$.extend($.fn,{
	attachupload:function(settings){
		settings = $.extend({
								url:getContextPath()+'/mvc/attachment/api/attachment/insertAttachment.json',
								attachTypeName:null,
								panelheight:50,
								data:{},
								name:'file',
								elementName:'file',
								region:'bottom',
								init:null,
								clear:false,
								callback:null
							},settings);
		$(this).attr("readonly","readonly").attr("disabled","disabled");
		$(this).data("settings",settings);
		if(settings.attachTypeName==null){
			throw new Error("missing attachTypeName config!");
		}
		
		 $.ajax({  
	         type: 'POST',
	         dataType: "json",
	         url:getContextPath()+'/mvc/attachment/api/attachmenttype/findAttachType.json',
	     	 data:{'name':settings.attachTypeName},
	         success: function(json){
	        	if(json){
						settings.filter=json.suffix;
						settings.max=json.num;
						settings.showType=json.showType;
				}
	          } 
	      });
		
		
		if(isNaN(settings.max)){
			settings.max=-1;
		}else if(settings.max==0){
			settings.max=-1;
		}
		
		$(this).each(function(){
			var $this = $(this);
			if(!$this.hasClass("jquery-upload"))$this.addClass("jquery-upload");
			$this.initPanel(settings);
			if(settings.init){
				$this.appendFiles(settings.init,settings);
			}
			var uploadder = 
				new AjaxUpload($this.next(),{
					action:settings.url,
					name: settings.name,
//					data: settings.data,
					autoSubmit: false,
					onChange: function(file, extension){
						if(settings.filter!=null && settings.filter!=""){
							var filter = settings.filter.split(";").join("|"),
								reg = new RegExp(filter);
							if(reg.test(extension)){
								$this.val(file);
								$this.next().next().removeAttr("disabled");
								uploadder.setData($.extend(settings.data,{'attachTypeName':settings.attachTypeName}));
								uploadder.setData($.extend(settings.data,{'fileType':extension}));
							}else{
								if($.messager)
									showErrorMessage("请选择格式为"+filter+"的文件");
								else
									showErrorMessage("请选择格式为"+filter+"的文件");
								$this.next().next().attr("disabled","disabled");
							}
						}else{
							$this.val(file);
							$this.next().next().removeAttr("disabled");
							uploadder.setData($.extend(settings.data,{'attachTypeName':settings.attachTypeName}));
							uploadder.setData($.extend(settings.data,{'fileType':extension}));
						}
					},					
					onSubmit: function(file, extension){
						loadStar();
						$this.next().next().attr("disabled","disabled");
					},
					onComplete: function(file, response){
						$this.val("");
						if(response.indexOf("success")!=-1){
//							response = response.substring(5).substring(0,response.indexOf("</pre")-5);
							var result = $.evalJSON(response);
							if(result.success){
								$this.data("fileId",result.msg);
								$this.appendFiles([{fileID:result.msg,fileName:file}],settings,uploadder);
								!!settings.callback?settings.callback.call($this,response):"";
							}else{
								showErrorMessage("文件"+file+"上传失败,原因:"+result.msg);
							}
						}else{
							showErrorMessage("文件"+file+"上传失败!");
						}
						loadEnd();		
					}
			});
			$this.data("uploadder",uploadder);
			$this.next().next().click(function(){
				uploadder.submit();
			});
			
			$("."+$this.data("element_id")).on('click', 'span', function(e){
				if($(this).attr('class')=='remove'){
					$this.removeFiles($(this).closest(".fileItem"),settings,uploadder);
				}
			});

			$("."+$this.data("element_id")).find(".clear").click(function(){
				$this.removeFiles($(this).closest(".uploaded").find(".fileItem"),settings,uploadder);
			});
			
			return $this;
		});	
	},
	initPanel:function(settings){
		var element_id = Math.floor(Math.random()*10000),$this=$(this),
			panel = $("<div class='uploaded "+element_id+"' style='z-index:9000'></div>").appendTo($this.parent());
		
		if(settings.region=="bottom"){
			panel.css({
				'height':settings.panelheight,
				'width':'80%',
				'overflow-x':'hidden',
				'overflow-y':'auto',
				'position':'absolute',
				'top':'22px',
				'left':$this.position().left
			});
		}else if(settings.region=="custom"){
			panel.css({
				'height':settings.panelheight,
				'overflow-x':'hidden',
				'overflow-y':'auto'
			});
		}else if(settings.region=="left"){
			panel.css({
				'height':settings.panelheight,
				'width':'80%',
				'overflow-x':'hidden',
				'overflow-y':'auto',
				'position':'absolute',
				//'top':$this.offset().top+$this.outerHeight(),
				//'left':$this.offset().left
				'top':$this.position().top,
				'left':$this.position().left+$this.outerWidth()+150
			});
		}else if(settings.region=="none"){
			panel.css('display','none');
		}

		$this.data("element_id",element_id).attr("ele_name",function(){return $(this).attr("name")||settings.elementName}).removeAttr("name");
		
		$("<a href='#' onclick='return false;' class='btn-review'>浏览</a>&nbsp;"+"&nbsp;<a href='#' onclick='return false;' class='btn-upload'>上传</a>").insertAfter($this);
		//.linkbutton();
		$("<input type='hidden' name='"+(settings.elementName==null?settings.name:settings.elementName)+"' class='upload-element'/>").insertBefore($this);
//		if(-1!=settings.max)
//			$("<span class='total'>还可以上传<span class='count'>"+settings.max+"</span>个文件</span>").appendTo(panel);
//		if(settings.clear)
//			$("<span class='clear' style='color:blue;cursor:pointer;float:right;'>clear</span>").appendTo(panel);
	},
	appendFiles:function(files,settings,uploadder){
		var $this = $(this);
		if(settings.region!="none"){
			$.each(files,function(i,o){
				var name = "";
				if(o.fileID!=null){
					$.ajax({
						url:getContextPath()+'/mvc/attachment/api/attachment/findAttachment.json',
						type:'post',
						cache:false,
						async:false,
						data:{'attachID':o.fileID,'attachTypeName':settings.attachTypeName},//o.fileID
						dataType:'json',
						success:function(result){
							if(result!=null){
								if(settings.showType=='1'){
									var url1=getContextPath()+'/mvc/attachment/api/attachment/downAttach.json?attachID='+result.attachID+'&attachTypeName='+settings.attachTypeName;
									name = '<img alt="" src="'+url1+'"  style="width:50px;height:50px;"/>';
								}else{
									name = '<a href="javascript:downFile(\'' + result.attachID +'\');"  title="'+result.fileName+'">'+result.fileName+'</a>';
								}		
							}
						}
					});
				}
				
				if(name!=""){
					if($.support.msie){
						if(settings.showType!='1'){
							$("<div class='fileItem' fileId='"+o.fileID+"' style='width:100%;margin-top:2px;' title='"+o.fileName+"'>"
									+"<span class='name' style='display:block;float:left;overflow:hidden;white-space:nowrap;width:50%;text-align:left;'>"+name+"</span>"
									+"<span class='remove' style='display:block;float:left;color:blue;cursor:pointer;'>删除</span></div>")
									.prependTo("."+$this.data("element_id"));
						}else{
							$("<div class='fileItem' fileId='"+o.fileID+"' style='width:65px;margin-top:2px;float:left;' title='"+o.fileName+"'>"
									+"<span class='name'>"+name+"</span>"
									+"<span class='remove' style='color:blue;cursor:pointer;'>X</span></div>")
									.prependTo("."+$this.data("element_id"));
						}
						
					}else{
						if(settings.showType!='1'){
							$("<div class='fileItem' fileId='"+o.fileID+"' style='width:100%;margin-top:2px;' title='"+o.fileName+"'>"
									+"<span class='name' style='display:display:block;float:left;overflow:hidden;white-space:nowrap;width:50%;text-align:left;'>"+name+"</span>"
									+"<span class='remove' style='display:block;float:left;color:blue;cursor:pointer;'>　删除</span></div>")
									.prependTo("."+$this.data("element_id"));
						}else{
							$("<div class='fileItem' fileId='"+o.fileID+"' style='width:65px;margin-top:2px;float:left;' title='"+o.fileName+"'>"
									+"<span class='name'>"+name+"</span>"
									+"<span class='remove' style='color:blue;cursor:pointer;'><a> X</a></span></div>")
									.prependTo("."+$this.data("element_id"));
						}
						
					}
					var temFileID=$(":hidden[name='"+settings.elementName+"']").val();
					if(temFileID!=''){
						var fileIDs=temFileID.split(',');
						if(fileIDs[0]==''){
							fileIDs[0]=o.fileID;	
						}else{
							if(fileIDs[0].indexOf(o.fileID)<0){
								fileIDs[0]=fileIDs[0]+";"+o.fileID;				
							}
						}
						$(":hidden[name='"+settings.elementName+"']").val(fileIDs.join(","));
					}else{
						$(":hidden[name='"+settings.elementName+"']").val(o.fileID+",");
					}
				}
			});
		}else{
			$this.val("上传成功:"+files[0].fileID);
		}
		$this.calCount(settings,uploadder);
	},
	removeFiles:function(files,settings,uploadder){
		var $this = this;
		$.each(files,function(i,o){
			var temFileID=$(":hidden[name='"+settings.elementName+"']").val();
			var fileIDs=temFileID.split(',');
			fileIDs[0]=fileIDs[0].replace($(o).attr("fileId"),"");
			
			if(fileIDs[1]==''){
				fileIDs[1]=$(o).attr("fileId");
			}else{
				if(fileIDs[1].indexOf(o.fileID)<0){
					fileIDs[1]=fileIDs[1]+";"+$(o).attr("fileId");
				}
			}
			
			$(":hidden[name='"+settings.elementName+"']").val(fileIDs.join(","));
			$(o).remove();
			$this.calCount(settings,uploadder);
			$this.removeData("fileId");
		});
	},
	calCount:function(settings,uploadder){
		if(uploadder == null){
			uploadder = $(this).data("uploadder");
		}
		$("."+$(this).data("element_id")).find(".count").text(settings.max-$("."+$(this).data("element_id")).find(".fileItem").length);
		if($("."+$(this).data("element_id")).find(".fileItem").length==settings.max && -1!=settings.max){
//			$(this).next().attr("disabled","disabled").next().attr("disabled","disabled");
			//$(this).next().linkbutton("disable");
			//$(this).next().next().linkbutton("disable");
			uploadder.disable();
		}else{
//			$(this).next().removeAttr("disabled").next().removeAttr("disabled");
			//$(this).next().linkbutton("enable");
			//$(this).next().next().linkbutton("enable");
			uploadder.enable();
		}
	},
	destroy:function(){
		$(this).data("uploadder").destroy();
	},
	getFileId:function(){
		var settings = $(this).data("settings");
		var temFileID=$(":hidden[name='"+settings.elementName+"']").val();
		return temFileID;
	},
	isEmpty:function(){
		var settings = $(this).data("settings");
		var temFileID=$(":hidden[name='"+settings.elementName+"']").val();
		if(temFileID!=''){
			var fileIDs=temFileID.split(',');
			if(fileIDs[0]==''){
				return true;
			}else{
				return false;
			}
		}else{
			return true;
		}
	},
	initFiles:function(fileId){
		if(fileId){
			$(this).reset();
			var fileids=fileId.split(";");
			for(var i=0;i<fileids.length;i++){	
				if(fileids[i]!=''){
					$(this).appendFiles([{fileID:fileids[i]}],$(this).data("settings"));
				}
			}	
		}
	},
	reset: function(){
		//console.log($(this).data('settings'));
		var $this = $(this);
		var uploadder = $this.data('uploadder');
		var settings = $(this).data("settings");
		var files = $("."+$this.data("element_id")).find(".remove").closest(".fileItem");
		
		$(":hidden[name='"+settings.elementName+"']").val("");
		$.each(files, function(i,o){ 
			$(o).remove();
			$this.calCount(settings,uploadder);
			$this.removeData("fileId");
		});
	}
});

$(function(){
	$(".jquery-upload").each(function(){
		$(this).attr("ele_name",function(){return $(this).attr("name")})
				.removeAttr("name")
					.attachupload({
							name:$(this).attr("fileName"),
							panelheight:$(this).attr("height"),
							elementName:$(this).attr("ele_name"),
							attachTypeName:$(this).attr("attachTypeName"),
							data:eval($(this).attr("data")),
							region:$(this).attr("region")
						});
	});
});
})(jQuery);


function initFileView(obj,fileID,attachTypeName) {
	if(fileID!=''){
		$.ajax({
			url:getContextPath()+'/mvc/attachment/api/attachment/findAttachment.json',
			type:'post',
			cache:false,
			async:false,
			data:{'attachID':fileID,'attachTypeName':attachTypeName},
			dataType:'json',
			success:function(result){
				if(result!=null){
					$("#"+obj).html('<a href="javascript:downFile(\'' + result.attachID +'\',\''+attachTypeName+'\');"  title="'+result.fileName+'">'+result.fileName+'</a>');
				}
			}
		});
	}else{
		$("#"+obj).html("");
	}
	
}

function initFilePdfView(obj,fileID,attachTypeName) {
	if(fileID!=''){
		var height=$("#"+obj).attr("height");
		var width=$("#"+obj).attr("width");
		$.ajax({
			url:getContextPath()+'/mvc/attachment/api/attachment/findAttachment.json',
			type:'post',
			cache:false,
			async:false,
			data:{'attachID':fileID,'attachTypeName':attachTypeName},
			dataType:'json',
			success:function(result){
				if(result!=null){
					if($.support.msie){
						height = Number(height) + Number(50);
						width = Number(width) + Number(30);
						var myPDF = new PDFObject({
							  url: result.fileUri,
							  width: width,
							  height: height
							}).embed(obj);
					}else{
						var myPDF = new PDFObject({
							  url: result.fileUri,
							  width: width,
							  height: height
							}).embed(obj);
					}
				}
			}
		});
	}
}


function getInitFile(fileId,attachTypeName) {
	var fileStr="";
	if(fileId!=null&&fileId!=''){
		var fj=fileId.split(";");
		$.each(fj, function(i, n){
			if(n!=''){
				$.ajax({
					url:getContextPath()+'/mvc/attachment/api/attachment/findAttachment.json',
					type:'post',
					cache:false,
					async:false,
					data:{'attachID':n,'attachTypeName':attachTypeName},
					dataType:'json',
					success:function(result){	
						if(result != null && result != '') 
							fileStr = fileStr+ '<a href="javascript:downFile(\'' + result.attachID +'\',\''+attachTypeName+'\');"  title="'+result.fileName+'">'+result.fileName+'</a>';
					}
				});
			}
		});
	}
	return fileStr;
}


function downFile(attachID,attachTypeName) {
	var url=getContextPath() + '/mvc/attachment/api/attachment/downAttach.json?attachID='+attachID+'&attachTypeName='+attachTypeName;;
	//$('#idown').attr("src", url); 
	CreateDownFrame(url);
}

function CreateDownFrame(url) {
	var oframe = $('<iframe id="downFrame">');  
	//修改样式是css，修改属性是attr  
	oframe.css("display", "none");  
	oframe.attr("src", url);  
	//在内部的前面加节点  
	$('body').prepend(oframe); 
}  

function subStr(str,percent){
	if(str == null)
		return null;
	var columnWidth = document.body.clientWidth*percent;
	var strLeng = strLength(str);
	if(strLeng*8 < columnWidth){
		return str;
	}else{
		var number = parseInt(columnWidth/8);
		var newStr = str.substring(0,number-5)+"...";
		return newStr;
	}	
}
function sublable(str,percent){
	if(str == null)
		return null;
	var newStr = subStr(str, percent);
	return '<label title=\''+str+'\'>'+newStr+'</label>';
	
}
function strLength(value){
	   var sum,i ;
	   sum=0;
	   for(i=0;i<value.length;i++) { 
		   if ((value.charCodeAt(i)>=0) && (value.charCodeAt(i)<299)) {
		      sum=sum+1;
		    }else {    		    
		      sum=sum+2;
		    }
	   }
	   return sum;
 }
